<template>
  <div class="courier">
    <div class="courier-name">{{file_name}}</div>
    <div class="courier-input flex-display flex-center-pack">
      <input type="text" class="flex-num-one" v-model="targetVlaue">
      <span @click="queryLog">查一下</span>
    </div>
    <div class="courier-result" v-if="codeList && codeList.length > 0">
      <div
        class="flex-display courier-result__item flex-center-align"
        v-for="(item, index) in codeList"
        :key="index"
        @click="selectCode(item)"
      >
        <span class="flex-num-one">单号：{{item.value}}</span>
        <img src="../../../static/img/gou.png" v-if="item.select">
      </div>
    </div>
    <div class="courier-reader" v-if="codeList && codeList.length">快递公司：
      <span>{{codeName }}</span>
      (不是{{codeName}}？
      <span @click="getUseKDCode">重新选择</span>)
    </div>
    <div class="result-box" v-if="courierLit && courierLit.length>0">
      <div class="result-success">
        <div id="resultTop" class="result-top">
          <span class="col1-up">时间</span>
          <span class="col2">地点和跟踪进度</span>
          <div role-slot="m_result_redpacket" class="red-packet"></div>
        </div>
        <ul class="result-list">
          <li :class="{'last': !index}" v-for="(item,index) in courierLit" :key="index">
            <div class="col1">
              <dl>
                <dt>{{item.ftime.split(' ')[0]}}</dt>
                <dd>{{item.ftime.split(' ')[1]}}</dd>
              </dl>
            </div>
            <div class="col2">
              <span></span>
            </div>
            <div class="col3">{{item.context}}</div>
          </li>
        </ul>
      </div>
    </div>
    <!--暂无查询结果的时候手动选择快递公司-->
    <div
      class="not-result flex-display flex-display-column flex-center-pack flex-center-align"
      v-if="isHasmore == false"
    >
      <img src="../../../static/img/not-data.png">
      <span>暂无查询结果</span>
      <span @click="getUseKDCode">可手动选择快递哦</span>
    </div>
    <mt-popup v-model="areaVisible" class="area-class" position="bottom">
      <div class="sureBtn flex-display flex-center-pack">
        <span @click="areaVisible = false">取消</span>
        <span @click="selectOtherLogistItem">确定</span>
      </div>
      <mt-picker :slots="slots" @change="onValuesChange"></mt-picker>
      <div class="tipTile">没有您合适的快递公司？可点击查看全部快递公司</div>
      <div class="show-log" @click="getKDCode">查看全部快递公司</div>
    </mt-popup>

    <div class="log-file flex-display flex-display-column animated fadeInRight" v-if="isLogistics">
      <div class="log-title">请选择快递
        <img src="../../../static/img/close.png" @click="isLogistics = false">
      </div>
      <div class="log-search flex-display flex-center-align">
        <i class="mintui mintui-search"></i>
        <input type="text" v-model="searchVal" class="flex-num-one">
      </div>
      <mt-index-list class="flex-num-one">
        <mt-index-section :index="item.title" v-for="(item, index) in logisticsList" :key="index">
          <div
            @click="selectLogistItem(childItem)"
            class="log-item"
            v-for="(childItem, childIndex) in item.cel"
            :key="childIndex"
          >{{childItem.value}}</div>
        </mt-index-section>
      </mt-index-list>
    </div>
  </div>
</template>
<script>
import Scroll from "@/base/scroll/scroll";
import Loading from "@/base/loading/loading";
import axios from "axios";
import { Toast, Picker, IndexList, IndexSection, Indicator } from "mint-ui";
export default {
  data() {
    return {
      targetVlaue: "", // 输入的目标索引
      isLoad: true, // 是否请求数据
      isHasmore: true, // 是否查询到数据
      codeList: null, // 查询到对应的值列表
      logisticsList: [], // 物流数据列表
      queryVal: "", // 当前查询到的值
      code: "yunda", // 当前选中物流公司
      codeName: "韵达物流", // 默认物流公司
      areaVisible: false, // 是否显示下拉框
      courierLit: [], // 获取快递公司列表
      isLogistics: false, // 是否显示快递列表
      file_name: "", // 文件名称
      searchVal: "", // 输入快递名称
      slots: [
        // 常用快递公司数组
        {
          flex: 1,
          values: [
            "圆通快递",
            "申通快递",
            "顺丰快递",
            "韵达快递",
            "德邦快递",
            "中通快递"
          ],
          className: "slot1",
          textAlign: "center"
        }
      ],
      usedList: [
        // 下拉选择快递公司
        {
          name: "yuantong",
          value: "圆通快递"
        },
        {
          name: "shentong",
          value: "申通快递"
        },
        {
          name: "shunfeng",
          value: "顺丰快递"
        },
        {
          name: "yunda",
          value: "韵达快递"
        },
        {
          name: "debangwuliu",
          value: "德邦快递"
        },
        {
          name: "zhongtong",
          value: "中通快递"
        },
        {
          name: "huitongkuaidi",
          value: "百世快递"
        },
        {
          name: "youzhengguonei",
          value: "邮政包裹"
        },
        {
          name: "ems",
          value: "EMS"
        },
        {
          name: "youzhengguoji",
          value: "邮政国际"
        }
      ]
    };
  },
  created() {
    // 设置分享到微信的配置
    let wxShareOpt = {
      title: this.$route.query.file_name,
      desc: "查询系统，一款永久免费的一键智能查询系统",
      link: window.location.href.split("#")[0],
      img: `${this.courierUrl}/${this.$route.query.QR_code}`
    };
    // 调用微信分享接口
    this.wxShare(wxShareOpt);
    // 获取文件名称
    this.getFileName();
  },
  methods: {
    // 从新选择快递
    getUseKDCode() {
      this.isHasmore = true;
      this.areaVisible = true;
    },
    // 获取文件名称
    getFileName() {
      axios
        .get("/choujiang/v1/rm/", {
          params: {
            file_id: this.$route.query.file_id
          }
        })
        .then(err => {})
        .catch(result => {
          if (result.status === 200) {
            this.file_name = result.data.file_name;
          }
        });
    },

    /**
     * @param {Object} item 当前点击对象
     * @desc 查询是否有快递信息
     */
    selectLogistItem(item) {
      this.isLogistics = false;
      this.code = item.name;
      this.codeName = item.value;
      this.courierLit = [];
      Indicator.open({
        text: "查询中...",
        spinnerType: "fading-circle"
      });
      axios
        .get("/choujiang/v1/query/", {
          params: {
            code: this.code,
            number: this.queryVal
          }
        })
        .then(err => {})
        .catch(result => {
          Indicator.close();
          if (result.data.msg) {
            Toast({
              message: "暂无记录请重新选择快递公司",
              duration: 2000
            });
            this.isHasmore = false;
            return;
          }
          if (result.status === 200) {
            Toast({
              message: "查询成功",
              duration: 1000
            });
            this.courierLit = result.data.data;
          }
        });
    },

    /**
     * @param {Object} item 当前点击对象
     * @desc 选择物流单号
     */
    selectCode(item) {
      this.isHasmore = true;
      Indicator.open({
        text: "查询中...",
        spinnerType: "fading-circle"
      });
      this.codeList.forEach(cel => {
        cel.select = cel.value == item.value ? true : false;
      });
      this.queryVal = item.value;
    },
    //  获取所有的物流公司
    getKDCode() {
      this.areaVisible = false;
      this.isHasmore = true;
      if (!this.logisticsList.length) {
        Indicator.open({
          text: "加载中...",
          spinnerType: "fading-circle"
        });
        axios
          .get("/choujiang/v1/getKDCode/")
          .then(res => {
            Indicator.close();
          })
          .catch(result => {
            console.log(result);
            Indicator.close();
            if (!result.data) {
              return;
            }
            let map = {};
            result.data.forEach((item, index) => {
              for (let k in item) {
                if (k == "code") {
                  let wlKey = item.code.substring(0, 1);
                  if (!map[wlKey]) {
                    map[wlKey] = {
                      title: wlKey,
                      cel: []
                    };
                  }
                  map[wlKey].cel.push({
                    name: item.code,
                    value: item.name
                  });
                }
              }
            });
            let ret = [];
            for (let key in map) {
              let val = map[key];
              ret.push(val);
            }
            ret.sort((a, b) => {
              return a.title.charCodeAt(0) - b.title.charCodeAt(0);
            });
            this.logisticsList = ret;
            this.otherLogList = ret;
            this.isLogistics = true;
          });
      } else {
        this.isLogistics = true;
      }
    },
    // 下拉组件改变事件
    onValuesChange(picker, values) {
      let obj = {
        name: this.usedList.filter(item => {
          return values[0] == item.value;
        })[0].name,
        value: values[0]
      };
      this.selectLogObj = obj;
    },
    // 点击确定选择快递公司
    selectOtherLogistItem() {
      this.areaVisible = false;
      this.selectLogistItem(this.selectLogObj);
    },

    // 查询快递信息进度
    queryLog() {
      Indicator.open({
        text: "查询中...",
        spinnerType: "fading-circle"
      });
      axios
        .get(
          `/choujiang/v1/file_inquire/?file_id=${
            this.$route.query.file_id
          }&target=${this.targetVlaue}`,
          {
            params: {
              code: this.code
            }
          }
        )
        .then(err => {
          console.log(err);
          console.log("错误信息");
          Toast({
            message: "目标不存在",
            duration: 1000
          });
          Indicator.close();
        })
        .catch(res => {
          console.log(res);
          Indicator.close();
          if (res.status === 200) {
            if (res.data.data && res.data.data.length) {
              res.data.data.forEach((element, index) => {
                element.select = !index ? true : false;
                for (let k in element) {
                  if (k != "select") {
                    element.value = element[k];
                  }
                }
              });
              this.queryVal = res.data.data[0].value;
              this.codeList = res.data.data;
            } else {
              Toast({
                message: res.data.error,
                duration: 1000
              });
              this.courierLit = [];
              Indicator.close();
            }
          }
        });
    }
  },
  watch: {
    // 监听输入的快递公司
    searchVal(val, oldVal) {
      let arr = [];
      let searchRegex = new RegExp(val, "i");
      this.logisticsList = this.otherLogList;
      this.logisticsList.forEach((item, index) => {
        arr[index] = {};
        arr[index].title = "";
        arr[index].cel = [];
        item.cel.forEach((childItem, childIndex) => {
          if (searchRegex.test(childItem.value)) {
            if (!arr[index].title) {
              arr[index].title = item.title;
            }
            arr[index].cel.push(childItem);
          }
        });
      });
      this.logisticsList = arr;
    },
    // 监听查询到的值
    queryVal(val, oldVal) {
      if (val) {
        this.courierLit = [];
        axios
          .get(`/choujiang/v1/query/`, {
            params: {
              code: this.code,
              number: this.queryVal
            }
          })
          .then(err => {
            Indicator.close();
          })
          .catch(result => {
            Indicator.close();
            if (result.data.msg) {
              this.isHasmore = false;
              return;
            }
            if (result.status === 200) {
              Toast({
                message: "查询成功",
                duration: 1000
              });
              this.courierLit = result.data.data;
            }
          });
      }
    }
  },
  components: {
    Scroll,
    Loading,
    Toast,
    Picker,
    IndexList,
    Indicator,
    IndexSection
  },
  beforeDestroy() {
    // 微信分享
    // var wxShareOpts = {
    //   title: "塞拉菲娜",
    //   desc:
    //     "塞拉菲娜是一家集生产、研发、智能科技美肤于一体的集团化公司，经营范围涉及生物技术推广服务、生物技术开发服务、生物产品的销售、生物产品的销售。",
    //   link: window.location.href.split("#")[0],
    //   img: "https://wap.sailafeinav.com/static/img/slfn-logo.379828f.jpg"
    // };
    // this.wxShare(wxShareOpts);
  }
};
</script>

<style lang="scss" scoped>
.sureBtn {
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
  border-bottom: 1px solid #e5e5e5;
  span {
    width: 30%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    display: inline-block;
    background: #0087ff;
    color: #fff;
    margin: 0 30px;
    border-radius: 3px;
  }
  span:first-child {
    background: #ccc;
  }
}
.show-log {
  width: 90%;
  height: 45px;
  line-height: 45px;
  margin: 0 auto;
  box-sizing: border-box;
  text-align: center;
  color: #fff;
  border-radius: 5px;
  background: #0087ff;
}
.tipTile {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: red;
}
.log-search {
  width: 100%;
  padding: 5px 0.6rem 5px 0.2rem;
  height: 40px;
  line-height: 40px;
  background: #fff;
  box-sizing: border-box;
  position: relative;
  i {
    position: absolute;
    left: 0.3rem;
  }
  input {
    outline: none;
    border-radius: 5px;
    width: 100%;
    height: 100%;
    padding-left: 0.6rem;
    border: 1px solid #e5e5e5;
  }
}
.log-title {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background: #fff;
  position: relative;
  border-bottom: 1px solid #e5e5e5;
  > img {
    position: absolute;
    width: 20px;
    height: 20px;
    position: absolute;
    right: 5px;
    top: 8px;
  }
}
.mint-indexlist {
  background: #fff;
  box-sizing: border-box;
}

.log-item {
  background: #fff;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-bottom: 1px solid #e5e5e5;
}
.log-file {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 100;
  padding: 20px 20px 0;
  width: 100%;
  background: rgba(0, 0, 0, 0.6);
}
.not-result {
  position: relative;
  > span {
    margin-top: 5px;
  }
  > span:last-child {
    color: #1973bf;
  }
}

.area-class {
  width: 100%;
  height: 50%;
}
.courier {
  width: 100%;
  height: 100%;
  .courier-name {
    width: 100%;
    height: 50px;
    font-size: 0.5rem;
    text-align: center;
    line-height: 50px;
  }
  .courier-input {
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    height: 45px;
    > input {
      background: #fff;
      border: 1px solid #ccc;
      padding-left: 10px;
    }
    > span {
      color: #fff;
      background-color: #5cb85c;
      border-color: #4cae4c;
      display: inline-block;
      line-height: 45px;
      padding: 0 10px;
    }
  }
  .courier-reader {
    width: 100%;
    height: 45px;
    background: #fff;
    width: 90%;
    margin: 20px auto;
    line-height: 45px;
    padding-left: 10px;
    overflow: hidden;
    border: 1px solid #ccc;
    span {
      color: #3781e4;
    }
  }
  .result-success {
    background: #fff;
  }

  .result-top {
    border-top: 1px solid #e6e6e6;
    border-bottom: 0;
    height: 43px;
    line-height: 43px;
    font-size: 16px;
    color: #5a5a5a;
    .col1-up {
      background: url(…9sjKKZfhJvyHYR1CVaGUBD0L1GqosYkQxETV+DroQECDAAHTBNKtk1K9cAAAAASUVORK5CYII=)
        60px 14px no-repeat;
      cursor: pointer;
      width: 3.25rem;
      padding-left: 25px;
      display: inline-block;
      margin-right: 25px;
    }
  }

  .result-list {
    border-top: 1px solid #e6e6e6;
  }

  .result-list li {
    display: -webkit-box;
    -webkit-box-align: center;
    overflow: hidden;
    color: #828282;
    border-bottom: 1px solid #e6e6e6;
  }

  .result-list.sortup .col2 span:before {
    border: 0;
    border-right: 1px solid;
    border-top: 1px solid;
    margin-top: 2px;
  }
  .result-list li.last {
    color: #ff7800;
  }
  .result-list li .col1 {
    padding: 0.3rem;
    text-align: center;
    font-size: 0.35rem;
    font-weight: bold;
    font-family: Helvetica, Arial, sans-serif;
  }
  ul,
  dl {
    list-style: none;
  }

  .result-list li .col1 dd {
    margin-top: 0.25rem;
  }
  .result-list li .col2 {
    width: 1.25rem;
    position: relative;
  }
  .result-list .col2 span {
    border: 1px solid #e6e6e6;
    border-radius: 50%;
    position: absolute;
    left: 0;
    top: 50%;
    margin-top: -0.5rem;
    width: 0.5rem;
    height: 0.5rem;
    background: #fff;
    z-index: 2;
    color: #e6e6e6;
  }
  .result-list .last .col2 span {
    border-color: #ff7800;
    color: #ff7800;
  }
  .result-list.sortup .col2 span:before {
    border: 0;
    border-right: 1px solid;
    border-top: 1px solid;
    margin-top: 10px;
  }
  .result-list .col2 span:before {
    position: absolute;
    top: 50%;
    left: 50%;
    content: "";
    width: 6px;
    height: 6px;
    border-left: 1px solid;
    border-bottom: 1px solid;
    -webkit-transform: translate(-50%, -50%) rotate(135deg);
    transform: translate(-50%, -50%) rotate(135deg);
  }

  .result-list li .col3 {
    -webkit-box-flex: 1;
    padding: 0.2rem;
    font-size: 0.35rem;
    line-height: 0.5rem;
    position: relative;
  }
  .result-list li:first-child .col3:before {
    top: 50%;
  }
  .result-list .col3:before {
    content: "";
    position: absolute;
    top: -1rem;
    bottom: -1rem;
    left: -38px;
    border-left: 1px solid #e6e6e6;
  }

  .courier-result {
    width: 90%;
    height: auto;
    padding: 0 20px;
    box-sizing: border-box;
    background: #fff;
    margin: 20px auto 0;
  }
  .courier-result__item {
    width: 100%;
    height: 1rem;
    border-bottom: 1px solid #e5e5e5;
    > span {
      display: block;
    }
    > img {
      width: 0.5rem;
      height: 0.5rem;
    }
  }
  .courier-result__item:last-child {
    border: none;
  }
}
</style>

